﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThereAreNoIrreplaceblePeople.Models;

namespace ThereAreNoIrreplaceblePeople.Controllers
{
    public class RequestController : Controller
    {
        //
        // GET: /Request/
        [Authorize]
        public ActionResult Index()
        {
            Requests requestsModel = new Requests();
            //Subject currentSubject;
            //foreach (Request r in requestsModel.AllRequests) {
            //    //currentSubject = requestsModel.Subjects.Find(s => s.CourseCode == int.Parse(r.SubjectName));
            //    r.SubjectName = r.SubjectName;//currentSubject.CourseName;
            //}

            requestsModel.AsSenderRequests = requestsModel.AllRequests.ToList().FindAll(r => r.SenderID == User.Identity.Name);
            requestsModel.AsReceiverRequests = requestsModel.AllRequests.ToList().FindAll(r => r.ReceiverID == User.Identity.Name);

          // Li st<Request> usersReqs = reqs.Where(r => r. == User.Identity. )
            return View(requestsModel);
        }


        public ActionResult Accept(int id, string guid)
        {
            Database1Entities3 db = new Database1Entities3();

            List<Request> reqs = db.Request.ToList();

            Request r = reqs.FirstOrDefault(a => a.RequestNumber == id && a.GUID == guid);

            if (r != null)
            {
                r.RequestStatus = 1;
                db.SaveChanges();

                //List<Subject> Subjects = db.Subject.ToList();
                //Subject currentSubject;
                //currentSubject = Subjects.Find(s => s.CourseCode == int.Parse(r.SubjectName));
                //r.SubjectName = currentSubject.CourseName;

                db.User.Single(a => a.Email == r.ReceiverID).HourCounter += r.NumberOfHours;
                db.User.Single(a => a.Email == r.SenderID).HourCounter -= r.NumberOfHours;
                db.SaveChanges();

                Mailing.MailHelper.SendEmail(r.SenderID, r.ReceiverID + " accepted your request!", r.ReceiverID + " accepted your request for subject " + r.SubjectName + " on " + r.Date + ".");

                return View(r);
            }

            return View();
        }

        public ActionResult Reject(int id, string guid)
        {
            Database1Entities3 db = new Database1Entities3();

            List<Request> reqs = db.Request.ToList();

            Request r = reqs.FirstOrDefault(a => a.RequestNumber == id && a.GUID == guid);

            if (r != null)
            {
                r.RequestStatus = 2;
                db.SaveChanges();

                //List<Subject> Subjects = db.Subject.ToList();
                //Subject currentSubject;
                //currentSubject = Subjects.Find(s => s.CourseCode == int.Parse(r.SubjectName));
                //r.SubjectName = currentSubject.CourseName;

                return View(r);
            }

            return View();
        }
    }
}
